package com.example.javamaday.leetcode;

import java.util.HashSet;
import java.util.Set;

public class LeetCodeTest0003 {

    public static void main(String[] args) {

    }

    public int lengthOfLongestSubstring(String s) {
        if (s == null) {
            return 0;
        }
        if (s.length() <= 1) {
            return s.length();
        }
        Set<Character> charSet = new HashSet<>();
        int n = s.length();
        int result = 0;
        int rightIndex = -1;
        for (int i = 0; i < n; i++) {
            // 控制左游标
            if (i != 0) {
                charSet.remove(s.charAt(i - 1));
            }
            // 右游标不断右移，直到出现重复
            while (rightIndex + 1 < n && !charSet.contains(s.charAt(rightIndex + 1))) {
                charSet.add(s.charAt(rightIndex + 1));
                rightIndex++;
            }
            result = Math.max(result, rightIndex - i + 1);
        }
        return result;
    }
}
